import { Injectable } from '@angular/core';
import { LeanCloudService } from 'app/utils/lean-cloud.service'
import { Observable } from 'rxjs'

@Injectable()
export class TeamManageService {

  constructor(
    private leanCloudService: LeanCloudService
  ) { }

  /**
   * 获取团队列表
   * @param page
   */
  getTeams(page) {
    return this.leanCloudService.send(
      ({ leancloud }) => {
        let query = leancloud.Query('team', page)
        query.include('admin')
        return query.find()
      })
  }

  /**
   * 获取团队信息
   */
  getTeam(id) {
    return this.leanCloudService.send(
      ({ leancloud }) => leancloud.Query('team').get(id))
  }

  /**
   * 保存团队数据
   * @param param0
   */
  saveTeam({ id, name, admin }: { id?, name?, admin?}) {
    var team = this.leanCloudService.Object.createWithoutData('team', id);

    name && team.set('name', name)
    admin && team.set('admin', admin)

    return team.save()
  }

  /**
   * 添加用户关系列表
   * @param teamId
   * @param users
   */
  addTeamUsers(team, users) {
    let TeamUserMap = this.leanCloudService.Object.extend('team_user_map')
    let maps = users.map(user => {
      let teamUserMap = new TeamUserMap()
      teamUserMap.set('team', team)
      teamUserMap.set('user', user)
      teamUserMap.set('admin', false)
      return teamUserMap
    })

    return this.leanCloudService.Object.saveAll(maps)
  }

  /**
   * 添加用户关系列表
   * @param teamId
   * @param users
   */
  addTeamProjects(team, projects) {
    let TeamProjectMap = this.leanCloudService.Object.extend('team_project_map')
    let maps = projects.map(project => {
      let teamProjectMap = new TeamProjectMap()
      teamProjectMap.set('team', team)
      teamProjectMap.set('project', project)
      return teamProjectMap
    })
    console.log(maps)
    return this.leanCloudService.Object.saveAll(maps).then((data) => { console.log(data); return data })
  }

  /**
   * 获取团队成员
   * @param team
   */
  getTeamUsers(team) {
    return this.leanCloudService.send(
      ({ leancloud }) => {
        let query = leancloud.Query('team_user_map')
        query.equalTo('team', team)
        return query.find()
          .then(data => data.map(item => item.get('user')))
          .then(data => this.leanCloudService.Object.fetchAll(data))
      }
    )
  }

  /**
   * 获取团队项目
   * @param team
   */
  getTeamProjects(team) {
    return this.leanCloudService.send(
      ({ leancloud }) => {
        let query = leancloud.Query('team_project_map')
        query.equalTo('team', team)
        return query.find()
          .then(data => data.map(item => item.get('project')))
          .then(data => this.leanCloudService.Object.fetchAll(data))
      }
    )
  }

  /**
   * 删除团队成员
   * @param team
   * @param user
   */
  deleteTeamUser(team, user) {
    let query = this.leanCloudService.Query('team_user_map')
    query.equalTo('team', team)
    query.equalTo('user', user)
    return this.leanCloudService.send(
      ({ leancloud }) => query.find()
        .then(data => this.leanCloudService.Object.destroyAll(data))
    )
  }

  /**
   * 删除团队项目
   * @param team
   * @param project
   */
  deleteTeamProject(team, project) {
    let query = this.leanCloudService.Query('team_user_map')
    query.equalTo('team', team)
    query.equalTo('project', project)
    return this.leanCloudService.send(
      ({ leancloud }) => query.find()
        .then(data => this.leanCloudService.Object.destroyAll(data))
    )
  }

  /**
   * 设置管理员
   */
  updateTeamUser(id) {
    this.leanCloudService.Object.createWithoutData('team')
  }
}
